package com.xgk.boot.module.core.dal.mapper.channel;

import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.mybatis.core.dataobject.BaseDO;
import com.xgk.boot.framework.mybatis.core.mapper.BaseMapperX;
import com.xgk.boot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageReqVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageRespVO;
import com.xgk.boot.module.core.controller.admin.permission.vo.role.RolePageReqVO;
import com.xgk.boot.module.core.dal.entity.channel.ApiServiceDO;
import com.xgk.boot.module.core.dal.entity.permission.RoleDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ApiServiceMapper extends BaseMapperX<ApiServiceDO> {

    default ApiServiceDO selectByName(String name) {
        return selectOne(ApiServiceDO::getAsName, name);
    }

    default ApiServiceDO selectByCode(String code) {
        return selectOne(ApiServiceDO::getAsCode, code);
    }

    default PageResult<ApiServiceDO> selectPage(ApiPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ApiServiceDO>().likeIfPresent(ApiServiceDO::getAsName, reqVO.getAsName()).likeIfPresent(ApiServiceDO::getAsCode, reqVO.getAsCode()).eqIfPresent(ApiServiceDO::getAsStatus, reqVO.getStatus()).betweenIfPresent(BaseDO::getCreateTime, new Instant[]{reqVO.getStartTime(), reqVO.getEndTime()}).orderByAsc(ApiServiceDO::getAsSort));
    }


    @Select("select a.* from api_service a ,service_provider_api_rela spp where a.as_id =spp.as_id and spp.sp_id = #{spId} and a.deleted =0  and a.as_status =0")
    List<ApiServiceDO> selectBySpId(@Param("spId") Long spId);

    @Select("select a.* from api_service a where  a.deleted =0 and  a.as_status =0 and a.as_id not in (select spp.as_id from " +
            "service_provider_api_rela spp where   spp.sp_id=#{spId})")
    List<ApiServiceDO> getApiSelectListNotInSp(@Param("spId") Long spId);

    @Select("SELECT a.* FROM api_service a " +
            "JOIN service_provider_api_rela r ON a.as_id = r.as_id " +
            "WHERE r.sp_id = #{providerId} AND a.as_status = 0  and a.deleted =0")
    List<ApiServiceDO> selectApisByProviderId(@Param("providerId") Long providerId);
}
